import { AnyAction, Reducer } from 'redux';
import { EffectsCommandMap } from 'dva';
import { message } from 'antd';

import {
  queryVIPUserManagementDetail,
  queryMyExpressListID,
  queryExpressList,
  querySaveAllExpressList,
  performSaveExpress,
  performDeleteExpress,
} from './service';

import { fetchValidation } from '@/utils';

export type Effect = (
  action: AnyAction,
  effects: EffectsCommandMap & { select: <T>(func: (state: {}) => T) => T },
) => void;

export interface ModelType {
  namespace: string;
  state: {};
  effects: {
    fetch: Effect;
    fetchMyExpressListID: Effect;
    fetchExpressList: Effect;
    saveAllExpressList: Effect;
    saveExpress: Effect;
    deleteExpress: Effect;
  };
  reducers: {
    save: Reducer;
    saveData: Reducer;
    lookup: Reducer;
    saveID: Reducer;
  };
}

const Model: ModelType = {
  namespace: 'VIPUserManagementExpress',

  state: {
    detailInfo: {},
    dataInfo: {
      count: 0,
      list: [],
    },
    allExpressList: {
      count: 0,
      list: [],
    },
    myExpressListID: {},
  },

  effects: {
    * fetch({ payload }, { call, put }) {
      const response = yield call(queryVIPUserManagementDetail, payload);
      yield put({
        type: 'save',
        payload: response.body,
      });
    },

    * fetchMyExpressListID({ payload }, { call, put }) {
      const response = yield call(queryMyExpressListID, payload);
      yield put({
        type: 'saveID',
        payload: response.body,
      });
    },

    * fetchExpressList({ payload }, { call, put }) {
      const response = yield call(queryExpressList, payload);
      yield put({
        type: 'saveData',
        payload: response.body || { count: 0, list: [] },
      });
    },

    * saveAllExpressList({ payload }, { call, put }) {
      const response = yield call(querySaveAllExpressList, payload);
      const data = fetchValidation(response);
      if (data) {
        yield put({
          type: 'lookup',
          payload: response.body,
        });
      }
    },

    * saveExpress({ payload: { searchValues, payload } }, { call, put }) {
      const response = yield call(performSaveExpress, { ...payload });
      const data = fetchValidation(response);

      if (data) {
        message.success('添加成功');
        yield put({
          type: 'fetchExpressList',
          payload: {
            ...searchValues,
          },
        });
        yield put({
          type: 'fetchMyExpressListID',
          payload: {
            vipId: payload.vipId,
          },
        });
      }
    },

    * deleteExpress({ payload: { searchValues, payload } }, { call, put }) {
      const response = yield call(performDeleteExpress, { ...payload });
      const data = fetchValidation(response);
      if (data) {
        message.success('删除成功');
        yield put({
          type: 'fetchExpressList',
          payload: {
            ...searchValues,
          },
        });
        yield put({
          type: 'fetchMyExpressListID',
          payload: {
            vipId: payload.vipId,
          },
        });
      }
    },
  },

  reducers: {
    save(state, action) {
      return {
        ...state,
        detailInfo: action.payload,
      };
    },

    saveData(state, action) {
      return ({
        ...state,
        dataInfo: action.payload,
      });
    },

    lookup(state, action) {
      return {
        ...state,
        allExpressList: action.payload,
      };
    },

    saveID(state, action) {
      return {
        ...state,
        myExpressListID: action.payload,
      };
    },
  },
};

export default Model;
